<template>
  <div>
    <!-- 左侧菜单 -->
    <div class="detail_box">
      <!-- 菜单 -->
      <div class="tab_menu">
        <div>
          <span v-for="(item,index) in menus" :class="{'choose':index===tabIndex}"
                @click="chooseTab(index)">{{ item }}订单</span>
        </div>
      </div>
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>仓库：</label>
            <el-select
              filterable
              v-model="warehouseId"
              placeholder="请选择仓库" @change="getSender">
              <el-option
                v-for="(item,index) in warehouseOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item" v-if="warehouseId">
            <label>配送员：</label>
            <el-select
              filterable
              clearable
              v-model="senderId"
              placeholder="请选择配送员">
              <el-option
                v-for="(item,index) in senderOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <label>出货单号：</label>
            <el-input clearable type="text" placeholder="请输入出货单号" v-model="keyword"></el-input>
          </div>
          <div class="operate_item">
            <label>时间：</label>
            <el-date-picker
              v-model="proDate"
              type="datetimerange"
              clearable
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span @click="openPopup('')" class="operate_btn search_btn">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="flex_box">
        <div class="detail_part" :class="{'left_box':isSee}">
          <div class="table_box">
            <el-table
              :data="tableInfo"
              :header-cell-style="tableHeader"
              border
              ref="multipleTable"
              style="width: 100%;"
              tooltip-effect="dark"
              v-loading="loading" @sort-change="getChange">
              <el-table-column
                label="ID"
                prop="id"
                width="80">
              </el-table-column>
              <el-table-column
                label="创建时间"
                prop="1"
                sortable="custom"
                width="160">
                <template slot-scope="scope">
                  <p style="text-align: left">{{ formatTime(scope.row.createTime) }}</p>
                </template>
              </el-table-column>
              <el-table-column
                label="单号"
                prop="deliveryNo">
              </el-table-column>
              <el-table-column
                label="仓库名称">
                <template slot-scope="scope">
                  <span>{{ scope.row.warehouseName ? scope.row.warehouseName : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="120"
                label="商品总数" prop="3" sortable="custom">
                <template slot-scope="scope">
                  <span>{{ scope.row.numbers ? scope.row.numbers : '0' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="150"
                label="配送员名称" prop="4" sortable="custom">
                <template slot-scope="scope">
                  <span>{{ scope.row.senderName ? scope.row.senderName : '——' }}</span>
                </template>
              </el-table-column>
              <el-table-column
                width="150"
                label="配送员电话">
                <template slot-scope="scope">
                  <span>{{ scope.row.senderPhone ? scope.row.senderPhone : '——' }}</span>
                </template>
              </el-table-column>

              <el-table-column
                width="100"
                label="状态" prop="2" sortable="custom">
                <template slot-scope="scope">
                  <span :style="'color:'+statusColorOption[scope.row.enable]">{{
                      statusOptions[scope.row.enable]
                    }}</span>
                </template>
              </el-table-column>

              <el-table-column
                fixed="right"
                label="操作"
                v-if="btn&&(btn.update||btn.delete||btn.replace||btn.confirm || btn.queryReserve || btn.queryReplace || btn.queryRecovery)"
                width="200">
                <template slot-scope="scope">
                  <div class="table_btn">
                    <span @click="function(){isSee=true;orderNo=scope.row.deliveryNo;itemId=scope.row.id}"
                          class="change_info" v-if="btn.detail">详情</span>
                    <span @click="openPopup(scope.row.id)" class="change_info" v-if="btn.update&&scope.row.enable===2">编辑</span>
                    <span @click="openPopup(scope.row.id,scope.row.deliveryNo)" class="change_info success_btn"
                          v-if="btn.confirm&&scope.row.enable===2">确认</span>
                    <span @click="function(){isDelivery=true;deliveryNo=scope.row.deliveryNo;wid=scope.row.warehouseId}" class="change_info await"
                          v-if="btn.replace&&(scope.row.enable === 1 || scope.row.recycleOrderEnable === 2)">换货</span>
                    <span @click="deleteItem(scope.row)" class="change_info delete_btn" v-if="btn.delete">删除</span>
                    <span @click="gotoOrder(scope.row.deliveryNo,1)" class="change_info"
                          v-if="btn.queryReserve">预约单</span>
                    <span @click="gotoOrder(scope.row.deliveryNo,2)" class="change_info"
                          v-if="btn.queryReplace">换货单</span>
                    <span @click="gotoOrder(scope.row.deliveryNo,3)" class="change_info"
                          v-if="btn.queryRecovery">回收单</span>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="record_page">
            <span class="page_count">共<i>{{ totals }}</i>条</span>
            <el-pagination
              :current-page="currentPage"
              :page-size="pageSize"
              :page-sizes="[15, 20, 30, 40]"
              :total="totals"
              @current-change="getTableInfo"
              @size-change="sizeChange"
              background
              layout="prev, pager, next, sizes,jumper">
            </el-pagination>
          </div>
        </div>
        <div class="right_box" v-if="isSee">
          <p class="page_title">{{ orderNo }}订单详情 <i class="detail_close" @click="closeDetail"></i></p>
          <detail :orderId="itemId"></detail>
          <p class="page_title">{{ orderNo }}订单日志 <i class="detail_close" @click="closeDetail"></i></p>
          <log :orderId="orderNo"></log>
        </div>
      </div>

    </div>
    <edit :mid="menuId" :isDefine="isDefine" @listenToChild="listenToEdit" v-if="isPop"></edit>
    <replace :delivery="deliveryNo" :wid="wid" v-if="isDelivery" @listenToChild="listenToEdit"></replace>
  </div>
</template>

<script>
import edit from "./edit";
import replace from "../replace/edit"
import log from './log/index'
import detail from './detail/index'
import commons from "@/utils/common";
import {getBtn} from "@/utils/btnAuth";
import {getDeliveryOrder, delDeliveryOrder} from "@/api/basic/warehouse/deliveryOrder";
import {getAllWarehouse} from "@/api/basic/warehouse";
import {getAllData} from "@/api/basic/warehouse/sender"
import {judgeRole} from "@/utils/warehouseAuth";
import {formatTree} from "@/utils/menuAuth";

export default {
  name: 'shipment',
  components: {
    edit,
    replace,
    log,
    detail,
  },
  data() {
    return {
      statusOptions: ['不可用', '可用', '新增'],
      statusColorOption: ['#e40000', '#67c430', '#5EACFC'],
      proDate: '',
      keyword: '',
      warehouseId: '',
      senderId: '',
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      warehouseOption: [],
      senderOption: [],
      menuId: '',
      isPop: false,
      isDelivery: false,
      isDefine: '',
      btn: {},
      roleHouse: {},
      deliveryNo: '',
      isSee: false,
      itemId: '',
      orderNo: '',
      wid: '',
      sortNumber: '',
      order: '',
      isDialog: false,
      recordNo: '',
      menus: ['待出货', '已完成'],
      tabIndex: 0,
      status: 2
    }
  },
  mounted() {
    this.roleHouse = judgeRole();
    this.warehouseOption = this.roleHouse.list;
    this.warehouseId = this.roleHouse.id;
    if (this.warehouseId) {
      this.getSender(this.warehouseId)
    }
    if (this.$route.query.cId) {
      this.recordNo = this.$route.query.cId;
    }
    if (this.$route.query.recycleNo) {
      this.keyword = this.$route.query.recycleNo;
    }
    this.btn = getBtn();
    if (this.btn.query) {
      this.getTableInfo(1);
    }
  },
  methods: {
    //监听列表排序
    getChange(val) {
      let sort = parseInt(val.prop)
      if (this.sortNumber < 0) {
        this.sortNumber = sort;
      } else {
        this.sortNumber = -sort;
      }
      this.getTableInfo(1);
    },

    chooseTab(index) {
      this.isSee = false;
      index === 0 ? this.status = 2 : this.status = 1
      this.tabIndex = index;
      this.getTableInfo(1);
    },

    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        keyword: that.keyword,
        warehouseId: that.warehouseId,
        senderId: that.senderId,
        limit: that.pageSize,
        page: page,
        sort: that.sortNumber,
        recordNo: that.recordNo,
        enable: that.status,
        startDate: that.proDate ? that.proDate[0] : '',
        endDate: that.proDate ? that.proDate[1] : '',
      };
      getDeliveryOrder(queryParams).then(res => {
        that.tableInfo = res.list ? res.list : [];
        that.totals = res.count;
        that.loading = false;
      }).catch((e) => {
        that.tableInfo = [];
        that.totals = 0;
        that.loading = false;
      });
    },

    resetTable() {
      this.keyword = '';
      this.senderId = '';
      this.pageSize = 15;
      this.sortNumber = '';
      this.proDate = '';
      this.warehouseId = this.roleHouse.id ? this.roleHouse.id : '';
      this.getTableInfo(1)
    },
    //关闭详情和日志
    closeDetail() {
      this.isSee = false;
    },
    //跳转相关页面
    gotoOrder(id, type) {
      let that = this;
      let url = type === 1 ? '/order/reserve' : (type === 2 ? '/warehouse/replace' : '/warehouse/recovery');
      let parent = type === 1 ? '订单管理' : '仓库管理';
      that.$router.push({
        path: url,
        query: {
          key: formatTree(parent, url),
          deliveryNo: id,
          username: id
        }
      })
    },

    //全部仓库
    getWarehouse() {
      let that = this;
      getAllWarehouse('').then(res => {
        that.warehouseOption = res ? res : [];
      }).catch((e) => {
        that.warehouseOption = [];
      })
    },

    // 获取配送员
    getSender(val) {
      let that = this;
      that.senderId = '';
      if (val) {
        let params = {
          warehouseId: val,
          isAll:1
        };
        getAllData(params).then(res => {
          that.senderOption = res ? res : [];
        }).catch((e) => {
          that.senderOption = [];
        })
      }
    },

    // 移除
    deleteItem(obj) {
      let that = this;
      that.$confirm('确定删除「' + obj.deliveryNo + '」订单信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDeliveryOrder(obj.id).then(res => {
          that.getTableInfo(that.currentPage);
          that.$message.success({
            message: '删除成功！',
            duration: 1500
          });
        }).catch((e) => {
        });
      }).catch(() => {
      });
    },

    // 打开弹窗
    openPopup(id,isDefine) {
      isDefine ? this.isDefine=isDefine : this.isDefine = false;
      this.isPop = true;
      this.menuId = id;
    },

    listenToEdit(e) {
      this.isPop = false;
      this.isDelivery = false;
      if (e.refresh) {
        this.getTableInfo(this.currentPage)
      }
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600;'
    },
  }
}
</script>

<style scoped>
.flex_box {
  display: flex;
}

.detail_part {
  width: 100%;
}

.left_box {
  width: 60%;
}

.right_box {
  width: 40%;
}

.page_title {
  font-weight: 600;
  padding-left: 20px;
  margin-bottom: 10px;
  font-size: 18px;
  position: relative;
}
</style>
